<template>
	<view class="test-wrapper">

		<view class="test-box" v-for="(item,index) in testdata" :key="index">


			<view class="test-header">
				<span style="font-weight: 700;">{{item.title}} </span>
				<view class="right">
					时长 : {{item.expire}} 分钟
				</view>
			</view>
			<view class="fen">
				<view class="fen-item">题目总数：{{item.question_count}} </view>
				<view class="fen-item">总分数：{{item.total_score}} </view>
				<view class="fen-item">及格分：{{item.pass_score}} </view>
			</view>
			<view class="footer">
				<u-modal :show="show" :content='content' showCancelButton @confirm="tiao()" @cancel="show=false">
				</u-modal>
				<view class="custom-style" @click="show1(item.is_test,item)" :disabled="item.is_test">
					{{item.is_test == true ? "你考过了" : "参加考试"}}
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		testlistList,
		testList,
	} from "../api/test.js"
	export default {
		data() {
			return {
				show: false,
				content: '是否要参加考试',
				obj: {
					page: 1,
					limit: 8,
				},
				testdata: [],
				count: 0,
				id: 0
			}
		},
		onLoad() {
			this.testlistList()
		},
		methods: {
			//点击进入考试
			show1(flag, item) {
				console.log(item.id, "id")
				this.id = item.id
				if (flag == true) {
					console.log("已经考过了")
				} else {
					this.show = true
				}
			},
			tiao() {
				uni.navigateTo({
					url: `../test/test?id=${this.id}`
				})
				console.log(this.id, "id")
				this.show = false
			},
			//渲染考试列表
			async testlistList() {
				const res = await testlistList(this.obj)
				console.log(res, "考试列表")
				this.count = Math.ceil(res.data.data.count / this.obj.limit)
				this.testdata = [...this.testdata, ...res.data.data.rows];
			}
		},
		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function() {
			//注意数据最好清空下，刷新后只展示第1页的内容
			this.testdata = []
			this.obj.page = 1
			this.testlistList();
		},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {
			console.log('到底了');
			console.log(this.obj.page, this.count);
			if (this.obj.page < this.count) {
				this.obj.page++
				this.testlistList()
			} else {
				wx.showToast({
					title: '到最后一页了',
				})
			}
		},
	}
</script>

<style lang="scss">
	.test-wrapper {
		width: 100%;
		background-color: #f5f5f3;

		.test-box {
			height: 270rpx;
			width: 100%;
			background-color: white;
			margin-top: 20rpx;
			display: flex;
			flex-direction: column;

			.test-header {
				padding: 20rpx;
				width: 100%;
				height: 70rpx;
				font-size: 40rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				box-sizing: border-box;

				.right {
					font-size: 29rpx;
					font-weight: 0 !important;
					color: red;
				}
			}

			.fen {
				margin-top: 10rpx;
				width: 100%;
				padding: 20rpx;
				box-sizing: border-box;
				height: 30rpx;
				display: flex;
				justify-content: space-around;

				.fen-item {
					width: 33.33%;
					height: 100%;
					display: flex;
					align-items: center;
					font-size: 20rpx;
					color: #333;
				}
			}

			.footer {
				width: 100%;
				height: 100%;
				padding: 20rpx;
				box-sizing: border-box;
				display: flex;
				justify-content: flex-end;
				align-items: center;

				.custom-style {
					width: 180rpx;
					height: 82rpx;
					color: white;
					font-size: 30rpx;
					border-radius: 10rpx;
					background-color: #5ccc84;
					display: flex;
					align-items: center;
					justify-content: center;
				}
			}
		}

	}
</style>
